#include "linked_list.h"

ll_iterator *ll_get_iterator(linked_list *ll)
{
	ll_iterator *i = (ll_iterator *)malloc(sizeof(ll_iterator));
	i->ll = ll;
	i->node = ll->first;
	return i;
}

void ll_next(ll_iterator *i)
{
	i->node = i->node->next;
}

int ll_is_end(const ll_iterator *i)
{
	return (i->node == NULL);
}

void *ll_get(const ll_iterator *i)
{
	if(i->node == NULL) return NULL;
	else return i->node->data;
}

void *ll_remove(ll_iterator *i, void free_f(void *))
{
	void *data;
	ll_node *aux = i->node;

	if(i->node == NULL) return NULL;
	data = i->node->data;

	if(i->node->previous != NULL) i->node->previous->next = i->node->next;
	if(i->node->next != NULL) i->node->next->previous = i->node->previous;

	if(i->ll->first == i->node) i->ll->first = i->node->next;
	if(i->ll->last == i->node) i->ll->last = i->node->previous;

	i->node = i->node->next;
	free(aux);
	if(free_f != NULL) free_f(data);
	(i->ll->lenght)--;

	return data;
}
